<?php

/**
 * Created by PhpStorm.
 * User: yandian
 * Date: 16/11/28
 * Time: 11:17
 */
namespace app\admin\widget;

use app\common\util\ValueBag;
use think\Controller;

class Parts extends Controller
{
    public function combo($model, $name, $defaultValue = '', $preItems = [], $hasEmpty = true, $valueField = 'id', $textField = 'title', $htmlAttr = [], $where = [], $required = false)
    {
        $list = M($model)->where($where)->field($valueField . ',' . $textField)->select();
        $this->assign('idField', $valueField);
        $this->assign('titleField', $textField);
        $this->assign('comboName', $name);
        if (!empty($preItems)) {
            $list = array_merge($preItems, $list);
        }
        $this->assign('model', $list);
        if (!is_array($defaultValue)) {
            $defaultValue = explode(',', strval($defaultValue));
        }
        $this->assign('defaultValue', $defaultValue);
        if (is_array($htmlAttr)) {
            if ($required) {
                $htmlAttr['required'] = 'required';
            }
            $strArr = [];
            foreach ($htmlAttr as $key => $value) {
                $strArr[] = $key . '="' . $value . '"';
            }
            $htmlAttr = implode(' ', $strArr);
        }
        $this->assign('hasEmpty', $hasEmpty);
        $this->assign('htmlAttr', $htmlAttr);
        return $this->fetch('widget/combobox');
    }

    public function cbs($model, $name, $value = [], $valueField = 'id', $textField = 'title')
    {
        $list = M($model)->field($valueField . ',' . $textField)->select();
        $this->assign('idField', $valueField);
        $this->assign('titleField', $textField);
        $this->assign('name', $name);
        if (is_string($value)) {
            $value = explode(',', $value);
        }
        $this->assign('model', $list);
        $this->assign('value', $value);
        return $this->fetch('widget/cbs');
    }

    public function siteCombo($name, $htmlAttr = [])
    {
        return $this->combo('site', $name,
            session('site_id'),
            [['id' => 0, 'title' => '主站']], false, 'id', 'title',
            $htmlAttr,
            [
                'id' => ['gt', 0]
            ]);
    }

    public function options($model, $idField = 'id', $titleField = 'title')
    {
        $list = M($model)->field($idField . ',' . $titleField)->select();
        $v = [];
        foreach ($list as $item) {
            $v[$item[$idField]] = $item[$titleField];
        }
        return json_encode($v);
    }

    public function siteOptions()
    {
        return $this->options('site');
    }

    public function siteField()
    {
        return "{field: 'site_id', width: 120, title: '所属站点', sortable: true,options:" . $this->siteOptions() . "}";
    }

    public function op($name, $key, $value = null, $hasEmpty = true, $htmlAttr = [], $required = true)
    {

        $options = ValueBag::getOptions($key);
        if (empty($options)) {
            $options = [];
        }
        if (is_array($htmlAttr)) {
            $html = '';
            foreach ($htmlAttr as $key => $v) {
                $html .= $key . '="' . $v . '"';
            }
            $htmlAttr = $html;
        }

        $this->assign([
            'name' => $name,
            'options' => $options,
            'value' => $value,
            'hasEmpty' => $hasEmpty,
            'required' => $required,
            'htmlAttr' => $htmlAttr
        ]);
        return $this->fetch('widget/op');
    }

    public function combotreeOfSite($model, $name, $value = '', $emptyValue = '', $emptyText = '--请选择--', $valueField = 'id', $textField = 'title', $parentField = 'parent_id')
    {
        return $this->combotree($model, $name, $value, $emptyValue, $emptyText, $valueField, $textField, $parentField, ['site_id' => (session('site_id') ?: 0)]);
    }

    public function combotree($model, $name, $value = '', $emptyValue = '', $emptyText = '--请选择--', $valueField = 'id', $textField = 'title', $parentField = 'parent_id', $where = [])
    {
        $rows = M($model)->field($valueField . ',' . $textField . ',' . $parentField)->where($where)->select();
        $temp = array();
        $childName = 'children';
        foreach ($rows as $m) {
            $pid = $m[$parentField];
            $id = $m[$valueField];
            if (isset($temp[$id])) {
                $m[$childName] = $temp[$id][$childName];
            }
            $temp[$id] = $m;
            if ($pid > 0 && $pid != $id) {
                if (!isset($temp[$pid])) {
                    $temp[$pid] = array();
                }
                if (!isset($temp[$pid][$childName])) {
                    $temp[$pid][$childName] = array();
                }
                $temp[$pid][$childName][] = &$temp[$id];
            }
        }
        $results = array();
        foreach ($temp as $key => $m) {
            if (!isset($m[$parentField]) || $m[$parentField] != 0 || $m[$parentField] == $m[$valueField]) {
                unset($temp[$key]);
            } else {
                $results[] = $m;
            }
        }
        echo '<select class="form-control" name="' . $name . '" required><option value="' . $emptyValue . '">' . $emptyText . '</option>';
        $func = function ($rows, $level) use (&$func, $valueField, $textField, $value) {
            foreach ($rows as $row) {
                $preFix = str_repeat('==', $level);
                if (!empty($preFix)) {
                    $preFix .= '> ';
                }
                echo '<option' . ($value == $row[$valueField] ? ' selected="selected"' : '') . ' value="' . $row[$valueField] . '">' . $preFix . $row[$textField] . '</option>';
                if (!empty($row['children'])) {
                    $func($row['children'], $level + 1);
                }
            }
        };
        $func($results, 0);
        echo '</select>';
        return '';
    }

    public function dictCombo($tag, $name = '', $value = '', $htmlAttr = [], $hasEmpty = false)
    {
        $name = empty($name) ? $tag : $name;
        $item_id = M('dict_items')->where(['tag' => $tag])->getField('id');
        if (empty($item_id)) {
            $item_id = 0;
        }
        $rows = M('dict_options')->field('value,text')->where(['item_id' => $item_id])->select();
        $this->assign('idField', 'value');
        $this->assign('titleField', 'text');
        $this->assign('comboName', $name);
        if (is_string($value)) {
            $value = explode(',', $value);
        }
        $this->assign('defaultValue', $value);
        $this->assign('model', $rows);
        $this->assign('hasEmpty', $hasEmpty);
        if (is_array($htmlAttr)) {
            $strArr = [];
            foreach ($htmlAttr as $key => $value) {
                $strArr[] = $key . '="' . $value . '"';
            }
            $htmlAttr = implode(' ', $strArr);
        }
        $this->assign('htmlAttr', $htmlAttr);
        return $this->fetch('widget/combobox');
    }
}